/*****************************************************************************************
 **  Author:jxx 2022
 **  QQ:283591387
 **完整文档见：http://v2.volcore.xyz/document/api 【代码生成页面ViewGrid】
 **常用示例见：http://v2.volcore.xyz/document/vueDev
 **后台操作见：http://v2.volcore.xyz/document/netCoreDev
 *****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码
import modelBody from "../operations_qd/operations_qd.vue";
import gridHeader from "@/components/print/selectTemplet.vue"
let extension = {
  components: {
    //查询界面扩展组件
    gridHeader,
    gridBody: '',
    gridFooter: '',
    //新建、编辑弹出框扩展组件
    modelHeader: '',
    modelBody: modelBody,
    modelFooter: ''
  },
  tableAction: '', //指定某张表的权限(这里填写表名,默认不用填写)
  buttons: {
    view: [],
    box: [],
    detail: []
  }, //扩展的按钮
  methods: {
    //下面这些方法可以保留也可以删除
    async onInit() { //框架初始化配置前，
      //示例：在按钮的最前面添加一个按钮
      //   this.buttons.unshift({  //也可以用push或者splice方法来修改buttons数组
      //     name: '按钮', //按钮名称
      //     icon: 'el-icon-document', //按钮图标vue2版本见iview文档icon，vue3版本见element ui文档icon(注意不是element puls文档)
      //     type: 'primary', //按钮样式vue2版本见iview文档button，vue3版本见element ui文档button
      //     onClick: function () {
      //       this.$Message.success('点击了按钮');
      //     }
      //   });

      //示例：设置修改新建、编辑弹出框字段标签的长度
      // this.boxOptions.labelWidth = 150;
      this.queryFields = ["Site", "Operations_date"]
      let startTime = this.base.getOneWeekAgoDate()
      let endTime = this.base.getDate(false) + " 23:59:59"
      this.searchFormFields.Operations_date = [startTime, endTime]
      this.detailOptions.textInline = false
      this.textInline = false
      // this.setFiexdSearchForm(true);
      // this.buttons.forEach(item => {
      //   if (item.name == "高级查询") {
      //     item.hidden = true
      //   }
      // })
      if (this.$route.path == "/q_operations" && JSON.stringify(this.$route.query) != '{}') {
        console.log("触发查询")
        await this.base.setSearchParamFn(this)
        // this.searchFormFields.Voucher_sub = null
        // this.searchFormFields.Operations_contract_sub = null
        // this.searchFormFields.Site = null
        // this.searchFormFields.Operations_state = null
        // this.searchFormFields.AuditStatus = null
        // this.searchFormFields.CreateDate = [null, null]
        // const selectList = ['Operations_state', 'AuditStatus', 'CreateDate',]
        // this.searchFormOptions.forEach(item => {
        //   item.forEach(x => {
        //     if (selectList.includes(x.field)) {
        //       x.onChange = (v) => {
        //         this.$route.query = {}
        //       }
        //     }
        //   })
        // })

        setTimeout(() => {
          this.refresh()
        })
      }
    },
    onInited() {
      //框架初始化配置后
      //如果要配置明细表,在此方法操作
      //this.detailOptions.columns.forEach(column=>{ });
      // 2025.3.14 设置明细表多张图片上传
      let column = this.detailOptions.columns.find(x => { 
        // console.log(x);console.log("查看X得值")
        return x.field == 'Image' 
      });
      column.edit.multiple=true;
      column.edit.maxFile=4;
      let indexNum = this.detailOptions.buttons.findIndex(item => item.name == "添加行")
      if (indexNum != "-1") {
        this.detailOptions.buttons.splice(indexNum, 1)
      }
      this.detailOptions.buttons.unshift({
        name: "选择运维服务",
        icon: 'el-icon-plus',
        type: 'primary',
        plain: true,
        onClick: function () {
          if (this.rowschild && this.$refs.detail.rowData.length) {
            this.rowschild.click()
          }

          this.$store.commit("setTabelText", true)
          if (!this.editFormFields.Site) {
            this.base.ElMessageFn("请选择站点")
            return false;
          }
          this.$refs.modelBody.open(this.editFormFields.Site)
        }
      })
      this.detailOptions.buttons.unshift({
        name: "异常维修",
        icon: 'el-icon-plus',
        type: 'primary',
        plain: true,
        onClick: function () {
          if (this.rowschild && this.$refs.detail.rowData.length) {
            this.rowschild.click()
          }
          const arr = this.$refs.detail.rowData.some(item => item.Code == 0 || item.Code == "0")
          // console.log("arr", arr)
          if (arr) {
            return this.base.ElMessageFn("明细表已存在异常维修项目");
          }
          const rowObj = {
            Code: 0,
            Name: '异常维修',
            Operations_date: this.base.getDate(false),
          }
          this.$refs.detail.rowData.push(rowObj)
        }
      })
      this.detailOptions.beginEdit = (row, column, index) => {
        if (this.$route.path != "/q_operations" || this.rowAuditStatus != 0) { // || !this.CreateUserBoole
          return false
        }
        return true
      }
    },
    async searchBefore(param) {
      //界面查询前,可以给param.wheres添加查询参数
      //返回false，则不会执行查询
      if (this.$route.path == "/q_operations" && JSON.stringify(this.$route.query) != '{}') {
        await this.base.setSearchBefore(this, "Operations", param)
        // const sectionState = ["[null,null]", JSON.stringify(["", null]), JSON.stringify([null, ""]), JSON.stringify(["", ""])]
        // const dateState = sectionState.includes(JSON.stringify(this.searchFormFields.CreateDate))
        // if (this.searchFormFields.Voucher_sub || this.searchFormFields.Operations_contract_sub || this.searchFormFields.Site || !dateState) {
        //   param.wheres = param.wheres.filter(item => item.name != "Operations")
        //   this.$route.query = {}
        // }
      }
      return true;
    },
    searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      return true;
    },
    addBefore(formData) {
      //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
      return true;
    },
    updateBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return true;
    },
    delBefore(ids, rows) { //查询界面的表删除前 ids为删除的id数组,rows删除的行
      let auditStatus = rows.some(x => {
        return x.AuditStatus == 1
      });
      if (auditStatus) {
        this.base.ElMessageFn('不支持删除审核通过数据')
        return false;
      }
      return true;
    },
    rowClick({
      row,
      column,
      event
    }) {
      //查询界面点击行事件
      this.$refs.table.$refs.table.toggleRowSelection(row); //单击行时选中当前行;
    },
    detailRowClick({
      row,
      column,
      event
    }) {
      // this.$refs.detail.$refs.table.toggleRowSelection(row);
      if (this.$route.path != "/q_operations" || this.rowAuditStatus != 0) {
        return
      } //|| !this.CreateUserBoole
      if (column.property == "Image" || column.property == "Attachment" || column.property == "Case" || column.property == "Remark") {
        let dom = event.target
        if (dom.nodeName != "SPAN") {
          setTimeout(() => {
            let inpDom = dom.querySelector("input")
            inpDom.focus()
          }, 0)
        }
      }
    },
    detailSelectable(row, index) {
      //设置界面CheckBox是否可以勾选
      //判断字段=xx的checkbox不能选中
      // 2025.3.14  设置运维服务单可以选中
      if (row.Code != 0) {
        return true;
      }
      return true;
    },
    onModelClose(iconClick) {
      //iconClick=true为点击左中上角X触发的关闭事件
      //如果返回 false不会关闭弹出框 
      //return false;
      // this.storeRemoveForm()
      this.$store.commit("setTabelText", true)
      this.boxModel = false;
    },
    async modelOpenBefore(row) {
      this.rowAuditStatus = await row.AuditStatus
      if (JSON.stringify(row) == "{}") {
        this.rowAuditStatus = 0
        this.CreateUserBoole = true
      }
      if (this.$route.path != "/q_operations" || this.rowAuditStatus != 0) { //|| !this.CreateUserBoole
        this.boxButtons[0].hidden = true
        this.detailOptions.buttons.forEach(item => {
          if (item.name != "导出" && item.name != "刷新") {
            item.hidden = true
          }
        })
      } else {
        this.boxButtons[0].hidden = false
        this.detailOptions.buttons.forEach(item => item.hidden = false)
      }
    },
    modelOpenAfter(row) {
      this.$store.commit("setTabelText", false)
      //点击编辑、新建按钮弹出框后，可以在此处写逻辑，如，从后台获取数据
      //(1)判断是编辑还是新建操作： this.currentAction=='Add';
      //(2)给弹出框设置默认值
      //(3)this.editFormFields.字段='xxx';
      //如果需要给下拉框设置默认值，请遍历this.editFormOptions找到字段配置对应data属性的key值
      //看不懂就把输出看：console.log(this.editFormOptions)
      if (this.currentAction == 'Add') {
        this.editFormFields.Operations_date = this.base.getDate(false)
      }

      this.editFormOptions.forEach(item => {
        item.forEach(x => {
          console.log(x.field);console.log("查看选中行")
          // if (x.field == 'Image' || x.field == 'Attachment') {
          //   //启用多图上传(默认单图)
          //   x.multiple = true;
          //   //最多可以上传3张照片
          //   x.maxFile = 3;
          // }
          // if (x.field == "Operations_contract") {
          //   x.hidden = true
          // }
          // if (x.field == "Operations_contract_sub" || x.field == "Voucher_sub" || x.field == "Site" || x.field == "Client_name") {
          if (x.field == "Voucher_sub" || x.field == "Operations_date") {
            x.disabled = true
            // if (x.field == "Operations_contract_sub") {
            //   x.remote = true
            //   x.extra = {
            //     icon: "el-icon-zoom-in",
            //     text: "选择运维合同",
            //     style: "color:#3a8ee6;font-size:13px;cursor:default",
            //     click: () => {
            //       this.$store.commit("setTabelText", true)
            //       this.$refs.modelBody.open()

            //     }
            //   }
            // }
          } else {
            x.disabled = false
          }
          // if (x.field == 'WHSE_Id') {
          //   const bool = !(this.currentAction == 'Add')
          //   x.disabled = bool
          // }
          if (this.$route.path != "/q_operations" || this.rowAuditStatus != 0) { //|| !this.CreateUserBoole
            x.disabled = true
          }
        })
      })
      this.detailOptions.columns.forEach(item => {
        if (item.field == "Image" || item.field == "Attachment") {
          if (this.$route.path != "/q_operations" || this.rowAuditStatus != 0) {
            item.readonly = true
          } else {
            item.readonly = false
          }
        }
      })
    }
  }
};
export default extension;